const mongoose = require('mongoose')
const Comment = mongoose.model('comment')
const Reply = mongoose.model('reply')
const { formatBody, sleep } = require('../tools')

module.exports = {
  // 评论列表
  async commentList (ctx) {
    let page = ctx.query.page || 1
    let limit = ctx.query.limit || 20
    let worksId = ctx.query.worksId
    let state = ctx.query.state
    let query = {}
    if (state) query.state = state
    if (worksId) query.worksId = worksId
    let list = await Comment.find(query, { fromUser: 1, content: 1, type: 1, state: 1, 'meta.createdAt': 1 })
      .populate([
        { path: 'fromUser', select: '_id nickname avatar' }
      ])
      .sort({ 'meta.createdAt': -1 })
      .limit(+limit).skip(+limit * (+page - 1))
      .lean() // lean作用 参考 https://blog.csdn.net/seaalan/article/details/83057449
    let total = await Comment.find(query).countDocuments()
    let Apromise = list.map(async item => {
      let replyList = await Reply.find({ commentId: item._id }, { fromUser: 1, toUser: 1, content: 1, state: 1, type: 1 })
        .populate([
          { path: 'fromUser', select: '_id nickname avatar' },
          { path: 'toUser', select: '_id nickname avatar' }
        ])
        .sort({ 'meta.createdAt': -1 })
        .limit(1)
      item.replyList = replyList
      item.replyTotal = await Reply.find({ commentId: item._id }).countDocuments()
      return item
    })
    let returnList = await Promise.all(Apromise)
    return ctx.body = formatBody(1, 'ok', { total, list: returnList })
  },
  // 评论详情
  async commentDetail (ctx) {
    let commentId = ctx.query.commentId
    let comment = await Comment.findOne({
      _id: commentId
    }).populate([
      { path: 'fromUser', select: '_id nickname avatar' }
    ])
    return ctx.body = formatBody(1, 'ok', comment)
  },
  // 回复列表
  async replyList (ctx) {
    let page = ctx.query.page || 1
    let limit = ctx.query.limit || 20
    let commentId = ctx.query.commentId
    let state = ctx.query.state
    let query = {}
    if (state) query.state = state
    if (commentId) query.commentId = commentId
    let list = await Reply.find({ commentId }, { fromUser: 1, toUser: 1, content: 1, state: 1, type: 1, 'meta.createdAt': 1 })
      .populate([
        { path: 'fromUser', select: '_id nickname avatar' },
        { path: 'toUser', select: '_id nickname avatar' }
      ])
      .sort({ 'meta.createdAt': -1 })
      .limit(+limit).skip(+limit * (+page - 1))
      .lean() // lean作用 参考 https://blog.csdn.net/seaalan/article/details/83057449
    let total = await Reply.find(query).countDocuments()
    return ctx.body = formatBody(1, 'ok', { total, list })
  },

  // 添加评论
  async addComment (ctx) {
    let { fromUser, content, type, worksId } = ctx.request.body
    let comment = new Comment({
      worksId,
      fromUser,
      content,
      type
    })
    await comment.save()
    return ctx.body = formatBody(1, '评论成功')
  },

  // 添加回复
  async addReply (ctx) {
    let { fromUser, toUser, content, type, commentId } = ctx.request.body
    let reply = new Reply({
      commentId,
      fromUser,
      toUser,
      content,
      type
    })
    await reply.save()
    return ctx.body = formatBody(1, '回复成功')
  }
}